<?php
error_reporting(0);
$isadmin=1;
include '../../comm/config.php';
include '../../comm/msgset.php';
include '../../comm/checkpostandget.php';
include '../check.php';

function sel_sql($table,$sel_field,$where,$alert=0){
	global $BIAOTOU;
	$sql="select $sel_field from $BIAOTOU$table where $where";
	if($alert==1){
	    echo $sql;
	}
	$query=mysql_query($sql);
	if($query){
		while($row=mysql_fetch_array($query)){
			if(strpos($sel_field,",") or $sel_field=="*"){
			    $arr=$row;
			}
			else{
			    $arr=$row[$sel_field];
			}
		}
	}
	return $arr;	
}


function update_sql($table,$field_arr,$where,$xs,$alert=0){ //$xs 0表示更新 1表示同加  2表示同减,$alert默认为0,1表示打印sql语句,2表示先执行在打印
    global $BIAOTOU;
	$sql="";
	foreach($field_arr as $k=>$v){
		if($xs==0){
		    $sql="`$k`='$v',".$sql;
		}
		elseif($xs==1){
		    $sql="`$k`=`$k`+'$v',".$sql;
		}
		elseif($xs==2){
		    $sql="`$k`=`$k`-'$v',".$sql;
		}
	}
	$sql=substr($sql,0,strlen($sql)-1);
	$sql="update $BIAOTOU$table set $sql where $where;";
	if($alert==0){
		mysql_query($sql);
	}
	elseif($alert==1){
		echo $sql;
	}
	elseif($alert==2){
		mysql_query($sql);
	    echo $sql;
	}
}

function insert_one_sql($table,$field_arr,$alert=0){
    global $BIAOTOU;
	$field="";
	$values="";
	foreach($field_arr as $k=>$v){
		$field = "`".$k."`,".$field;
		$values = "'".$v."',".$values;
		}
	$field=substr($field,0,strlen($field)-1);
	$values=substr($values,0,strlen($values)-1);
	$sql="insert into $BIAOTOU$table($field) values ($values);";
	$query=mysql_query($sql);
	if($alert==0){
	    if($query) return 1;
	    else return mysql_error();
	}
	elseif($alert==1){
	    echo $sql;
	}
	elseif($alert==2){
		echo $sql;
	    if($query) return 1;
	    else return mysql_error();
	}
}

function inc_fanli($ddusername, $fxje, $tgje, $order_code,$merchant_id) {
	$field_arr=array('money'=>$fxje,'dengji'=>1);
	update_sql('user', $field_arr, "ddusername='$ddusername'", 1); //增加会员金额和等级

	//用户消息
	$title = '您获得了新的商城交易返现！';
	$trade_id = $order_code;
	$msg_tabao = "您获得了新的交易返现，".$merchant_id."商城订单号" . $order_code . "返现金额" . $fxje;
	$filed_arr = array (
		'title' => $title,
		'content' => $msg_tabao,
		'addtime' => date('Y-m-d H:i:s'
	), 'see' => 0, 'ddusername' => $ddusername, 'senduser' => '网站客服');
	insert_one_sql("msg", $filed_arr);

	//用户明细
	$shijian = "商城交易返现";
	$memo = $merchant_id."交易号$order_code";
	$filed_arr = array (
		'ddusername' => $ddusername,
		'shijian' => $shijian,
		'addtime' => date('Y-m-d H:i:s'
	), 'je' => $fxje, 'memo' => $memo);
	insert_one_sql("mingxi", $filed_arr);

	//求推荐人
	$tjrid = sel_sql("user", "tjr", "ddusername='$ddusername'");
	if ($tjrid > 0) {
		$tjrname = sel_sql("user", "ddusername", "Id='$tjrid'");
		//增加推荐人佣金
		$field_arr = array (
			'money' => $tgje
		);
		update_sql("user", $field_arr, "Id='$tjrid'", 1);

		//用户消息
		$title = '您获得了新的推广佣金！';
		$msg_taobaotuiguang = "您获得了新的推广佣金" . $tgje;
		$filed_arr = array (
			'title' => $title,
			'content' => $msg_taobaotuiguang,
			'addtime' => date('Y-m-d H:i:s'
		), 'see' => 0, 'ddusername' => $tjrname, 'senduser' => '网站客服');
		insert_one_sql("msg", $filed_arr);

		//用户明细
		$shijian = "推广佣金";
		$memo = "交易人$ddusername";
		$filed_arr = array (
			'ddusername' => $tjrname,
			'shijian' => $shijian,
			'addtime' => date('Y-m-d H:i:s'
		), 'je' => $tgje, 'memo' => $memo);
		insert_one_sql("mingxi", $filed_arr);
	}
}

$link = mysql_connect($dbserver, $dbuser, $dbpass);
if (!$link) {
	die('数据库链接出错');exit;
}
mysql_select_db($dbname);
mysql_query("set names utf8");

$id=$_POST['id'];
$tradeuser=$_POST['tradeuser'];

if($id==""){
	echo "<script language=javascript>alert('错误：没有要结算的数据！');window.location='mallorders.php'</script>";
    exit;
}

if($tradeuser==""){
	echo "<script language=javascript>alert('错误：没有要确认的会员！');window.location='mallorders.php'</script>";
    exit;
}

$dduser=sel_sql('user','Id,dengji',"ddusername='$tradeuser'");
$dduserid=$dduser['Id']?$dduser['Id']:0;
$dduserdengji=$dduser['dengji']?$dduser['dengji']:0;
$ddusername=$tradeuser;

$mall_order=sel_sql('mall_order','commission,order_code,merchant_id,stat_desc,u_id',"id='$id'");
$fxje=mall_ck_fxje($mall_order["commission"],$dduserdengji);
$tgje=$mall_order["commission"]*TGBL; 
$order_code=$mall_order["order_code"];
$merchant_id=$mall_order["merchant_id"];
$stat_desc=$mall_order["stat_desc"];
$u_id=$mall_order["u_id"];

if($order_code==''){
	echo "<script language=javascript>alert('错误：未找到该记录！');window.location='mallorders.php'</script>";
	exit;
}

if(($u_id=='' or $u_id==0) and $order_code!=''){
	$field_arr_order=array('u_id'=>$dduserid,'ddusername'=>$ddusername);
	update_sql('mall_order', $field_arr_order,"id='$id'",0);
	echo "<script language=javascript>alert('确认会员！');window.location='mallorders.php'</script>";
	exit;
}

if($dduserid>0){
	if($order_code!='' and $stat_desc=='未核对'){
	    $field_arr_order=array('stat_desc'=>'核对有效','qrsj'=>date('Y-m-d H:i:s'));
	    update_sql('mall_order', $field_arr_order,"id='$id'",0);
        inc_fanli($ddusername, $fxje, $tgje, $order_code, $merchant_id);
	}
}
else{
    $field_arr_order=array('stat_desc'=>'核对有效','qrsj'=>date('Y-m-d H:i:s'));
	update_sql('mall_order', $field_arr_order,"id='$id'",0);
}


echo "<script language=javascript>alert('确认返现成功，即将进入交易列表！');window.location='mallorders.php'</script>";
 
mysql_close($link);
?>